
finp$="picture.jpg"

xed=288:yed=192

'xed=0:yed=0:open finp$ for input as #1:for i=1 to 16:n=readbyte(1):next:for i=1 to 2:n=readbyte(1):next
'xed=readbyte(1)*256:xed=xed+readbyte(1):for i=1 to 2:n=readbyte(1):next:yed=readbyte(1)*256:yed=yed+readbyte(1)
'close #1

xed2=8+(xed*1.15)


setdisplay(xed2,yed+8,32,1):paper(8^8-1):ink(0):pen(0):cls

while 0=0

shell("fswebcam -d /dev/video0 _tmpr1.jpg")
shell("convert _tmpr1.jpg -resize 288x192\\! _tmpr2.jpg")
shell("sync && rm _tmpr1.jpg")
finp$="_tmpr2.jpg"

Loadimage(finp$,1):pasteicon(0,4,1)

dim clust[8,8] '-,o4b[2]

'-- define cluster
' a$="00,12,03,15,08,04,11,07,02,14,01,13,10,06,09,05"
  a$="00,06,08,14,02,12,04,10,08,14,00,06,04,10,02,12"
for y2=0 to 3:for x2=0 to 3:lc=(y2*4)+x2
clust[x2,y2]=val(mid$(a$,(lc*3)+1,2))
next:next

dim cor[16]
cor[0]=0x000000:cor[1]=0x0000FF
cor[2]=0xFF0000:cor[3]=0xFF00FF
cor[4]=0x00FF00:cor[5]=0x00FFFF
cor[6]=0xFFFF00:cor[7]=0xFFFFFF


for y=0 to yed+8 
  for xq=0 to xed2
     ind=point(xq,y)
        b= ind mod 256: ind=int(ind/256)
        g= ind mod 256: ind=int(ind/256)
        r= ind mod 256

      patgf=(((clust[xq mod 4,y mod 4]+1)*255)/16)
      rpat=0:if r>patgf then:rpat=1:end if
      gpat=0:if g>patgf then:gpat=1:end if
      bpat=0:if b>patgf then:bpat=1:end if
       o4b=(bpat+(rpat*2)+(gpat*4)) mod 8
   ink(cor[o4b]):dot(xq,y)
 next:next 

'- corrigir leitura de cores (?)

for y=0 to yed
      v0=0
  for x1=0 to int(xed/7)+1
        cflc=0
    for x2=0 to 6
        x=x2+(x1*7)
        apv=0 : cfl=0
        ind=point(x,y+4)
        b= ind mod 256: ind=int(ind/256)
        g= ind mod 256: ind=int(ind/256)
        r= ind mod 256
       prim=   (int(g/128))*4  +(int(r/128))*2  +(int(b/128)) 
          if prim=0 then : apv=0 : cfl=1 : end if   '- k
          if prim=3 then : apv=1 : cfl=0 : end if   '- m
          if prim=1 or prim=5 then : apv=1 : cfl=2 : end if   '- b
          if prim=4 then : apv=2 : cfl=0 : end if  '- g
          if prim=2 or prim=6 then : apv=2 : cfl=2 : end if   '- o
          if prim=7 then : apv=3 : cfl=1 : end if   '- w

       if(x mod 2)=0 then 
        if apv=0 or apv=2 then
          ink(0):dot (x,3)
         end if 
        if apv=0 or apv=1 then 
          ink(0):dot (x+1,3)
         end if 
       end if 


            cflc=cflc+cfl
    next
      if cflc<8 then
        ink(0):dot (xed+x1,3)
      end if 
  next


     ink(0):line(0,2,xed2,2)

    ink(0xFF00FF)
  for x=0 to xed-1 step 2
    i=point(x,3) mod 256
    if i>127 then:dot(x,2): end if
  next
    ink(0x00FF00)
  for x=1 to xed-1 step 2
    i=point(x,3) mod 256
    if i>127 then:dot(x,2): end if
  next
    ink(0xFF00FF)
  for x=0 to xed-1 step 2
    i1=point(x,3) mod 256:i2=point(x+2,3) mod 256
    if i1>127 and i2>127 then:dot(x+1,2): end if
  next
    ink(0x00FF00)
  for x=1 to xed-1 step 2
    i1=point(x,3) mod 256:i2=point(x+2,3) mod 256
    if i1>127 and i2>127 then:dot(x+1,2): end if
  next
    ink(0xFFFFFF)
  for x=1 to xed-1
    i1=point(x,3) mod 256:i2=point(x+1,3) mod 256
    if i1>127 and i2>127 then:dot(x,2):dot(x+1,2): end if
  next

    ink(0x000000)
  for x=1 to xed-1
    i0=point(x,2):i1=point(x+1,2):i2=point(x+2,2)
    if i0=0xFFFFFF and i2=0xFFFFFF and i1<>0xFFFFFF then:dot(x+1,2): end if
  next



  for x1=0 to int(xed/7)+1
      q=point(xed+x1,3) mod 256
    for x2=0 to 6
      n=point(x1*7+x2,2)
      if n=0xFF00FF and q>128 then: n=0x0000FF: end if
      if n=0x00FF00 and q>128 then: n=0xFF7F00: end if
      ink(n):dot(x1*7+x2,2)
    next
  next


    for x1=0 to xed-1
     q=point(x1,2)
     ink(q):dot(x1,y+4)
    next


     ink(0xffffff):line(0,3,xed2,3)
     ink(0xffffff):line(0,2,xed2,2)



next

fout$=finp$+".bmp":fout2$=finp$+".png"
grab (1,0,4,xed,yed):saveimage(fout$,1)
shell("convert "+fout$+" "+fout2$)
shell("rm "+fout$ )

wait(3000)

wend

